/**
 * This Class will initiate the VCP program flow of events.
 * @author Gil Levinson
 * @author Boris Fradlis
 * @author Yogev Levi
 * @author Hila Porat
 * 
 */

// Imports *******************************

package system;
import java.awt.Component;
import java.awt.SystemColor;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import control.LoginController;
import control.MessageController;
import gui.CustomerServiceWorker.CustomerServiceGUI;
import gui.Login.UserGUI;
import gui.NetworkManager.NetworkManagerGUI;
import gui.ParkingLotManager.ParkingLotManagerGUI;
import gui.ParkingLotWorker.*;
import gui.Server.ServerGUI;
import gui.client.ClientGUI;
import vcpClasses.Notification;
import vcpClasses.ParkingLot;
import vcpClasses.User;
import java.awt.Toolkit;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.Vector;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


// Start of VCP ****************************

public class VCP {
	
	/**
	 * Class Static variables ****************  
	 */
	
	public static JFrame frame;
	public static Date currDateTime;
	public static UserGUI loginPanel;
	public static VcpClient client = null;
	public static User currentUser = null;
	public static ParkingLot currentLot = null;
	public static UserGUI vcpFrame;
	public static int guiX = 300, guiY = 200;
	public static NetworkManagerGUI networkManagerGUI;
	public static ParkingLotManagerGUI parkingLotManagerGUI;
	public static ParkingLotWorkerGUI parkingLotWorkerGUI;
	public static CustomerServiceGUI customerServiceGUI;
	public static ClientGUI clientGUI;
	public static JPanel prevScreen;
	public static MessageController messageController;
	

	/**
	 * Here we will start flow of events.
	 * @param args - not used as run will not be initiated from command line.
	 */
	
	public static void main(String[] args) {
		
		//====== UserGUI (Login screen) ======//
		frame = new JFrame();
		frame.setTitle("VCP Client v1.0");
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent e) {
				Terminate();
				System.exit(0);
			}
		});
		frame.setIconImage(Toolkit.getDefaultToolkit().getImage(VCP.class.getResource("/img/Icon.png")));
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocation(guiX, guiY);
		frame.setVisible(true);
		
		Action logout = new AbstractAction()
		{
		    public void actionPerformed(ActionEvent e)
		    {
				JOptionPane.showMessageDialog(null, "You have been automatically logged out.\nPlease retart the system in order to use it again.", "Important",
					     JOptionPane.INFORMATION_MESSAGE);
				Terminate();
		    }
		};
		 

		InactivityListener listener = new InactivityListener(frame, logout, 10);
		listener.start();
		
		
		/** add Login Screen to panel frame**/
		loginPanel = new UserGUI();
		setScreen(null,loginPanel);
		//===============//
	} // End of main
	
	/**
	 * this method will handle termination of the program
	 */
	
	public static void Terminate()
	{
		SystemTypes sysType = new SystemTypes(VCP.currentUser,SystemMode.LOGOUT);
		boolean flag = LoginController.LogOut(sysType);
		if (!flag)
			 JOptionPane.showMessageDialog(null, "Logout Status May not be Updated in the DB!!!", "Error",JOptionPane.ERROR_MESSAGE);
		VCP.currentUser = null;
		try {
			VCP.client.terminateConnection();
		} catch (Exception e) {
			 JOptionPane.showMessageDialog(null, "Client Termination may not done succesfully!!!", "Error",JOptionPane.ERROR_MESSAGE);
		}
		VCP.client = null;
		System.exit(0);
	}
	
	/**
	 * this method will change screens for the client side of the program
	 * @param oldComp - old screen
	 * @param newComp - new screen
	 */
	
	public static void setScreen(Component oldComp, Component newComp)
	{		
		prevScreen = (JPanel) oldComp;
		if (oldComp != null)
		{
			oldComp.setVisible(false);
			frame.remove(oldComp);
		}
		if (newComp != null)
		{
			frame.getContentPane().add(newComp);
			frame.setLocation(guiX,guiY);
			frame.setSize(newComp.getSize());
			newComp.setVisible(true);
		}
	}
	
	/**
	 * this method the the current date and time in specific format
	 * @return the string  the current date and time 
	 */
	
	public static String getDateTime(){
		DateFormat dateFormat = new SimpleDateFormat("EEE, MMM d, yyyy",Locale.US);
		currDateTime = new Date();
		String dateTime = new String(dateFormat.format(currDateTime));
		return dateTime;
	}
	
	/**
	 * this method will set the screen from the initial screen to the view screen of the user by his privilege
	 * @param usrType - the privilege of the user
	 * @param thisComp - the initial screen
	 */
	
	public static void setGUI(int usrType,Component thisComp)
	{
		Component comp = null;
		switch(usrType)
		{
			case User.NET_MANAGER:
			{
				comp = networkManagerGUI = new NetworkManagerGUI();
				break;
			}
			case User.LOT_MANAGER:
			{
				comp = parkingLotManagerGUI = new ParkingLotManagerGUI();
				break;
			}
			case User.LOT_WORKER:
			{
				comp = parkingLotWorkerGUI = new ParkingLotWorkerGUI();
				break;
			}
			case User.CUST_SER_WORKER:
			{
				comp = customerServiceGUI = new CustomerServiceGUI();
				break;
			}
			case User.CLIENT:
			{
				comp = clientGUI = new ClientGUI();
				break;
			}
			default:
			{
				comp = loginPanel = new UserGUI();
			}
		}
		setScreen(thisComp,comp);		
	}
} // End of class VCP
